<template>
  <el-form
    :model="form"
    ref="form"
    :rules="rule"
    status-icon
    label-width="100px"
    label-position="left"
  >
    <el-form-item prop="password1" label="新密码">
      <el-input type="password" v-model="form.password1"></el-input>
    </el-form-item>
    <el-form-item prop="password2" label="确认密码">
      <el-input type="password" v-model="form.password2"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="warning" @click="prevStep">上一步</el-button>
      <el-button type="primary" :disabled="loading" :loading="loading" @click="submitPwd('form')">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { userAPI } from "@/utils/api.js";
import { mapGetters } from "vuex";
export default {
  data() {
    //自定义密码验证规则
    let validatePass1 = (rule, value, callback) => {
        if (value === "") {
          callback(new Error("请输入新密码"));
        } else {
          let passReg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/; //密码正则
          if (!passReg.test(value)) {
            callback(new Error("密码只能包含字母和数字"));
          } else if (this.form.password2 !== "") {
            this.$refs.form.validateField("password2");
          }
          callback();
        }
      },
      validatePass2 = (rule, value, callback) => {
        if (this.form.password1 === "") {
          this.$refs.form.validateField("password1");
        } else {
          if (value === "") {
            callback(new Error("请再次输入密码"));
          } else if (value !== this.form.password1) {
            callback(new Error("两次输入密码不一致!"));
          } else {
            callback();
          }
        }
      };
    return {
      loading: false, //点击提交显示加载中,并禁用
      /* 重置密码表单 */
      form: {
        password1: "",
        password2: ""
      },
      //表单验证规则
      rule: {
        password1: [
          { required: true, validator: validatePass1, trigger: "blur" },
          { max: 16, min: 6, message: "密码长度为6-16位", trigger: "blur" }
        ],
        password2: [
          { required: true, validator: validatePass2, trigger: "blur" },
          { max: 16, min: 6, message: "密码长度为6-16位", trigger: "blur" }
        ]
      }
    };
  },
  computed: {
    ...mapGetters({
      //vuex辅助函数 混入计算属性
      account: "getFrgAccount",
      captcha: "getFrgCaptcha"
    })
  },
  methods: {
    prevStep() {
      //上一步
      this.$emit("prevStep");
    },
    submitPwd(formName) {
      /* 提交用户名表单 */
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.loading = true;
          let data = {
            captcha: this.captcha,
            username: this.account,
            newPassword: this.form.password1
          };

          userAPI.forget
            .changeForgetPassword(data)
            .then(res => {
              //console.log(res);
              //返回登录表单
              setInterval(() => {
                this.$emit("backLogin");
                this.$emit('toStart');
              }, 1000);
              this.$message({
                type: "success",
                message: "注册成功,快去登录吧！",
                duration: 1000
              });
            })
            .catch(err => {
              console.log(err);
            })
            .finally(() => {
              this.loading = false; //取消加载和禁用
            });
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
</style>